import {Body, Controller, Get, Param, Post, Put, Query, Request, UseGuards} from "@nestjs/common";
import {ApiBody, ApiOperation, ApiQuery, ApiResponse, ApiTags} from "@nestjs/swagger";
import {UserService} from "./user.service";
import {EmailDto, UserDto, UserLoginDto, UserRegisterDto} from "./user.dto";
import {UserEntity} from "./user.entity";
import {LocalAuthGuard} from "../../common/guards/localAuth.guard";
import {JwtAuthGuard} from "../../common/guards/jwtAuth.guard";
import {Public} from "../../common/decorators/public.decorators";


@ApiTags("用户模块")
@Controller("user")
export class UserController {
    constructor(
        private readonly userService: UserService
    ) {
    }

    @Get("userInfo")
    @UseGuards(JwtAuthGuard)
    @ApiOperation({summary: "获取用户信息"})
    @ApiResponse({
        type: UserDto
    })
    async getInfo(
        @Request() req
        // @Query("username") username:string
    ): Promise<UserEntity> {
        const {user: {username}} = req;
        return await this.userService.findOne(username);
    }

    @Public()
    @Get("code")
    @ApiOperation({summary: "获取验证码(邮箱)"})
    async getCode(
        @Query() emailDto: EmailDto
    ) {
        const {email} = emailDto
        return await this.userService.sendMail(email);
    }

    @Post("register")
    @Public()
    @ApiOperation({summary: "用户注册"})
    @ApiBody({
        type: UserRegisterDto
    })
    register(
        @Body() body: UserRegisterDto
    ) {
        return this.userService.addOne(body);
    }

    @Post("login")
    @Public()
    @UseGuards(LocalAuthGuard)
    @ApiOperation({summary: "登录"})
    @ApiBody({
        type: UserLoginDto
    })
    login(
        @Request() req
    ) {
        const {user} = req;
        return this.userService.login(user);
    }

    @Get("firstLogin")
    @ApiOperation({summary: "判断用户是否首次登录"})
    isFirstLogin(
        @Request() req
    ) {
        const {user: {userId}} = req;
        return this.userService.isFirstLogin(userId);
    }

    @Post("logout")
    @ApiOperation({summary: "退出登录"})
    logout(
        @Request() req
    ) {
        const {user: {userId}} = req;
        return this.userService.logout(userId);
    }


    @Put("changePassword")
    @ApiOperation({summary: "修改密码"})
    changePassword() {
        return "";
    }


}
